/**
 * create by young (young2f@outlook.com) on 2020/10/30
 */


'use strict';
import React from 'react';
import {Button, Cascader, Checkbox, DatePicker, Form, Input, InputNumber, notification} from 'antd';
import {FormField, FormLayout, ModalFooter} from 'antd-extra';
import ComponentBase from '../../components/Base/ComponentBase';
import notify from '../../components/NotificationExtra';

const utils = require('../../utils/utils');

const moment = require('moment');
@Form.create()

export default class ModalCreateTenant extends ComponentBase {
  constructor(props) {
    super(props);
    this.state = {};
  }

  componentDidMount() {
    this.onLoadData().then(res => this.setState(res));
  }

  async onLoadData() {
    const {tenant, dispatch} = this.props;
    const code = tenant ? tenant.tenantCode : await dispatch({
      type: 'global/getCode',
      payload: {prefix: 'TEN'},
    }).then(res => res.code);
    console.log('tenant code', code);
    const zoneRes = await dispatch({
      type: 'global/getZone',
    });
    console.log('getZone', zoneRes);
    const zone = zoneRes && zoneRes.status === 'ok' ? zoneRes.result : undefined;
    return {code, zone};
  }

  renderFoot = () => {
    const {foot, value, modalRef} = this.props;
    if (foot) {
      return foot(value, modalRef);
    } else {
      return (
        <ModalFooter>
          <Button type="primary" onClick={() => {
            this.props.form.validateFields((err, values) => {
              if (err) {
                const {code} = err;
                const {errors} = code;
                errors.forEach(item => {
                  notification.error(item);
                });
                return;
              }
              const tenantCode = this.props.form.getFieldValue('tenantCode');
              this.props.dispatch({
                type: 'user/findUser',
                payload: {tenantCode, username: values.tenantAdminUser},
              })
                .then(res => {
                  const {result} = res;
                  if (result && result.length) {
                    notification.error({message: '管理员用户名已存在！'});
                    return;
                  }
                  const {tenantAdminPassword, tenantShortName} = values;
                  if (new Buffer(tenantShortName).length > 18) {
                    notify({type: 'error', message: '简称字数太长'});
                    return;
                  }
                  const pwd = this.getCryptoPassword(tenantAdminPassword);
                  values.tenantAdminPassword = pwd;
                  values.isChildren = false;
                  values.tenantType = 'Libra';
                  // values.maxUser = 20;
                  values.menu = utils.getAdminMenuKeys();
                  const loginInfo = wStore.getStore('loginInfo');
                  values.createUser = loginInfo ? loginInfo.username : undefined;
                  values.createUsername = loginInfo ? loginInfo.nickname : undefined;
                  console.log('创建租户', values);
                  this.props.result(values);
                  modalRef.close();
                });

            });
          }}>确定</Button>
          <Button onClick={() => {
            modalRef.close();
          }}>取消</Button>
        </ModalFooter>
      );
    }
  };

  render() {
    const {code, zone} = this.state;
    const {tenant = {}, form} = this.props;
    const isEdit = this.props.tenant;
    const {
      tenantName = '', tenantShortName = '', tenantCode = code, tenantAdminUser = '', maxRelatingTenant = 20, maxUser = 20, tenantAdminUsername = '',
      tenantAdminPassword = '', tenantInvalidData, tenantCity = '',isTenantSuperUser=false
    } = tenant;
    const data = moment().add(1, 'years');

    let _tenantInvalidData = tenantInvalidData ? moment(tenantInvalidData) : data;
    return (
      <div>
        <FormLayout form={form} cols={2}>
          <FormField
            label={'租户编码'}
            name={'tenantCode'}
            required={true}
            initialValue={tenantCode}
          >
            <Input disabled={true} style={{width: '90%'}}></Input>
          </FormField>
          <FormField
            label={'租户名称'}
            name={'tenantName'}
            required={true}
            initialValue={tenantName}
          >
            <Input style={{width: '90%'}}></Input>
          </FormField>
          <FormField
            label={'租户简称'}
            name={'tenantShortName'}
            required={true}
            initialValue={tenantShortName}
          >
            <Input style={{width: '90%'}}></Input>
          </FormField>
          <FormField
            label={'行政区域'}
            name={'tenantCity'}
            // required={true}
            initialValue={tenantCity}
          >
            <Cascader
              style={{width: '100%'}}
              options={zone}
              placeholder="请选择"
            />
          </FormField>
          <FormField
            label={'失效时间'}
            name={'tenantInvalidData'}
            initialValue={_tenantInvalidData}

            placeholder="请选择"
          >
            <DatePicker style={{width: '90%'}}></DatePicker>
          </FormField>
          <FormField
            label={'管理员账户'}
            name={'tenantAdminUser'}
            required={true}
            initialValue={tenantAdminUser}
            placeholder="请输入"
          >
            <Input disabled={isEdit} style={{width: '90%'}}></Input>
          </FormField>
          <FormField
            label={'管理员姓名'}
            name={'tenantAdminUsername'}
            required={true}
            initialValue={tenantAdminUsername}
            placeholder="请输入"
          >
            <Input  style={{width: '90%'}}></Input>
          </FormField>
          <FormField
            label={'管理员密码'}
            name={'tenantAdminPassword'}
            required={!isEdit}
            placeholder="请输入"
          >
            <Input disabled={isEdit} style={{width: '90%'}}></Input>
          </FormField>
          <FormField
            label={'最大用户数量'}
            name={'maxUser'}
            initialValue={maxUser}
            required={true}
            placeholder="请输入"
          >
            <InputNumber style={{width: '90%'}}></InputNumber>
          </FormField>
          <FormField
            label={'关联租户数量'}
            name={'maxRelatingTenant'}
            initialValue={maxRelatingTenant}
            required={true}
            placeholder="请输入"
          >
            <InputNumber style={{width: '90%'}}></InputNumber>
          </FormField>
          <FormField
            label={'租户超级管理员'}
            name={'isTenantSuperUser'}
            initialValue={isTenantSuperUser}
            required={true}
          >
            <Checkbox style={{width: '90%'}}></Checkbox>
          </FormField>
        </FormLayout>
        {this.renderFoot()}
      </div>
    );
  }
}
